package com.leetcode.LC;

public class LC1668 {
    public int maxRepeating(String sequence, String word) {
        //关键字替换
        if (!sequence.contains(word)) {
            return 0;
        }
        String replace = sequence.replace(word, "*");
        System.out.println(replace);
        int max = 0, tmp = 0;
        for (int i = 0; i < replace.length(); i++) {
            if (replace.charAt(i) == '*') {
                tmp++;
            } else {
                max = Math.max(max, tmp);
                tmp = 0;
            }
            max = Math.max(max, tmp);
        }

        StringBuilder builder = new StringBuilder(sequence).reverse();
        StringBuilder builder1 = new StringBuilder(word).reverse();
        String replace1 = builder.toString().replace(builder1.toString(), "*");
        System.out.println(replace1);
        int max1 = 0, tmp1 = 0;
        for (int i = 0; i < replace1.length(); i++) {
            if (replace1.charAt(i) == '*') {
                tmp1++;
            } else {
                max1 = Math.max(max1, tmp1);
                tmp1 = 0;
            }
            max1 = Math.max(max1, tmp1);
        }
        return Math.max(max,max1);
    }

    public int maxRepeating2(String sequence, String word) {
        int count=0;
        String tmp=word;
        while(sequence.contains(word)){
            word+=tmp;
            count++;
        }
        return count;
    }

    public static void main(String[] args) {
        System.out.println(new LC1668().maxRepeating("aaabaaaabaaabaaaabaaaabaaaabaaaaba", "aaaba"));
    }
}
